package com.sangfor.ssl;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.common.VpnCommon;
import com.sangfor.ssl.easyapp.SangforAuthForward;
import com.sangfor.ssl.service.setting.SettingManager;

/* loaded from: classes2.dex */
public class TicketAuthManager {
    private static final String CIPHER_SUITE = "sslCipherSuite";
    private static final String GM_VERSION = "gmVersion";
    private static final String SSL_ALGOR = "sslALgor";
    private static final String TAG = "TicketAuthManager";
    private static final String TICKET_CODE = "ticketCode";
    private static final String TICKET_ENABLE = "ticketEnable";
    private static final String TICKET_SP_NAME = "ticketInfo";
    private static final String TICKET_VPN_ADDRESS = "ticketVpnAddress";
    private Context mContext;
    private boolean mInited;
    private String mTicketCode;
    private boolean mTicketEnable;
    private String mVpnAddress;

    private TicketAuthManager() {
        this.mContext = null;
        this.mInited = false;
        this.mVpnAddress = null;
        this.mTicketCode = null;
        this.mTicketEnable = false;
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static class SingletonHodler {
        public static final TicketAuthManager INSTANCE = new TicketAuthManager();

        private SingletonHodler() {
        }
    }

    public static TicketAuthManager getInstance() {
        return SingletonHodler.INSTANCE;
    }

    public String getmVpnAddress() {
        return this.mVpnAddress;
    }

    public String getmTicketCode() {
        return this.mTicketCode;
    }

    public boolean ismTicketEnable() {
        return this.mTicketEnable;
    }

    public void init(Context context) {
        this.mInited = true;
        Context applicationContext = context.getApplicationContext();
        this.mContext = applicationContext;
        SharedPreferences sharedPreferences = applicationContext.getSharedPreferences(TICKET_SP_NAME, 0);
        this.mVpnAddress = sharedPreferences.getString(TICKET_VPN_ADDRESS, "");
        this.mTicketEnable = sharedPreferences.getBoolean(TICKET_ENABLE, false);
        String string = sharedPreferences.getString(TICKET_CODE, "");
        if (TextUtils.isEmpty(string)) {
            return;
        }
        this.mTicketCode = VpnCommon.decryptInfo(string, this.mContext);
    }

    public void clearTicketInfo() {
        this.mInited = false;
        this.mTicketEnable = false;
        this.mVpnAddress = null;
        this.mTicketCode = null;
        Context context = this.mContext;
        if (context != null) {
            context.getSharedPreferences(TICKET_SP_NAME, 0).edit().clear().commit();
        }
    }

    public void saveTicketInfo(boolean z) {
        Log.info(TAG, "save ticket info");
        this.mTicketCode = SangforAuthForward.getInstance().getTicketInfo("auth.ticket.key");
        String ticketInfo = SangforAuthForward.getInstance().getTicketInfo(IVpnDelegate.KEY_AUTH_TICKETENABLE);
        this.mVpnAddress = (String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_URL_KEY);
        this.mTicketEnable = "1".equals(ticketInfo);
        if (z) {
            if (this.mContext == null) {
                this.mContext = SangforAuthForward.getInstance().getContext();
            }
            Context context = this.mContext;
            if (context != null) {
                SharedPreferences.Editor edit = context.getSharedPreferences(TICKET_SP_NAME, 0).edit();
                String encryptInfo = VpnCommon.encryptInfo(this.mTicketCode, this.mContext);
                edit.putString(TICKET_VPN_ADDRESS, this.mVpnAddress);
                edit.putBoolean(TICKET_ENABLE, this.mTicketEnable);
                edit.putString(TICKET_CODE, encryptInfo);
                edit.apply();
            }
        }
    }

    public boolean ticketAuthAvailable(Context context) {
        if (!this.mInited) {
            init(context);
        }
        if (TextUtils.isEmpty(this.mVpnAddress)) {
            Log.error(TAG, "VpnAddress is empty");
            return false;
        } else if (TextUtils.isEmpty(this.mTicketCode)) {
            Log.error(TAG, "mTicketCode is empty");
            return false;
        } else {
            return this.mTicketEnable;
        }
    }
}
